<?php


namespace app\api\controller;


use think\facade\Request;

class User
{
    /**
     * 授权后校验用户是否绑定了手机号码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkUserLevel(){
        $openid = Request::param('openid','');
        if (empty($openid)){wapAjaxReturn(400,'请传入openid');}

        $data = \app\common\model\User::where(['openid'=>$openid])->find();
        if($data == null){wapAjaxReturn(400,'当前openid无效');}

        $status = 1;//1-未绑定手机号码  2-修改密码
        if(!empty($data['user_phone'])){$status = 2;}
        wapAjaxReturn(0,'success',['status'=>$status]);
    }

    /**
     * 用户绑定手机号码和初始密码设置
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bindPhone(){
        $post = Request::post();
        if (empty($post['user_phone'])){wapAjaxReturn(400,'请填写手机号码');}
        if(!preg_match('/^1\d{10}$/',$post['user_phone'])){wapAjaxReturn(400,'手机格式不正确');}
        if (empty($post['user_password'])){wapAjaxReturn(400,'初始密码不能为空');}
        if (!preg_match('/^[0-9a-zA-Z]{6,12}$/',$post['user_password'])){wapAjaxReturn(400,'密码只能是数字、大小写字母，且长度在6-12位之间');}
        if (empty($post['openid'])){wapAjaxReturn(400,'请传入openid');}
        $data = \app\common\model\User::where(['openid'=>$post['openid']])->find();
        if($data == null){wapAjaxReturn(400,'当前openid无效');}

        //判断当前手机是否被绑定过
        $userInfo = \app\common\model\User::where(['user_phone'=>$post['user_phone']])->find();
        if($userInfo){wapAjaxReturn(400,'当前手机号码已被绑定');}

        $post['user_pwd_salt'] = GetRandStr(4);
        $post['user_password'] = md5($post['user_password'] . 'das!ds329%3' . $post['user_pwd_salt']);
        $data->allowField(true)->save($post,['user_id'=>$data['user_id']]);

        $data->isUpdate() ? wapAjaxReturn(0,'绑定成功'):wapAjaxReturn(400,'绑定失败');
    }

    /**
     * 用户重置密码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editUserPassword(){
        $post = Request::post();
        if (empty($post['user_password'])){wapAjaxReturn(400,'新密码不能为空');}
        if (!preg_match('/^[0-9a-zA-Z]{6,12}$/',$post['user_password'])){wapAjaxReturn(400,'密码只能是数字、大小写字母，且长度在6-12位之间');}
        if (empty($post['openid'])){wapAjaxReturn(400,'请传入openid');}
        $data = \app\common\model\User::where(['openid'=>$post['openid']])->find();
        if($data == null){wapAjaxReturn(400,'当前openid无效');}

        $post['user_pwd_salt'] = GetRandStr(4);
        $post['user_password'] = md5($post['user_password'] . 'das!ds329%3' . $post['user_pwd_salt']);
        $data->allowField(true)->save($post,['user_id'=>$data['user_id']]);

        $data->isUpdate() ? wapAjaxReturn(0,'修改密码成功'):wapAjaxReturn(400,'修改失败');

    }
}